package com.flower.auth.infra.basic.config;

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.concurrent.Executor;
import java.util.logging.Logger;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-07-11 12:15
 */
@Intercepts({
        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class,
                RowBounds.class, ResultHandler.class,
        CacheKey.class, })
})
public class SqlStatementInterceptor implements Interceptor {

    public static final Logger log = Logger.getLogger(SqlStatementInterceptor.class.getName());

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        long startTime = System.currentTimeMillis();
        try {
            Object result = invocation.proceed();

            return result;
        }finally {
            long endTime = System.currentTimeMillis() - startTime;
            log.info("sql执行时间：" + endTime + "ms");

        }
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

}
